using Godot;
public class FurnaceBlock : BlockMaterial
{
    public FurnaceBlock() : base("furnace")
    {
        DisPlayName = "熔炉";
        StateMax = 2;
        RotateMax = 3;
        Collide = false;
        TickEvent = Tick;
        SetBlockEntityCreate(() => new FurnaceBlockEntity());
    }
    public override bool OnUse(PlayerUseBlockEvent e)
    {
        FurnaceView fv = GD.Load<PackedScene>("res://Tscn/Store/FurnaceView.tscn").Instance<FurnaceView>();
        fv.SetBlock(e.block);
        e.player.view.SetOpenView(fv);
        return true;
    }
    public void Tick(UpdataTickEvent e)
    {
        FurnaceBlockEntity fbe = (FurnaceBlockEntity)e.block.blockEntity;
        if (fbe.RecipeId == -1)
        {
            Recipe recipe = Materials.GetRecipe(Materials.GetBlockMaterial("furnace"), fbe.store.GetItemStack(0));
            if (recipe != null)
            {
                fbe.RecipeId = recipe.id;
                fbe.store.SubAmount(0, 1);
            }
        }

        if (fbe.RecipeId != -1)
        {
            if (fbe.FuelTick > 0)
            {
                e.block.SetState(1);
                fbe.FuelTick--;
                if (fbe.Pace >= FurnaceBlockEntity.MaxPace)
                {
                    Recipe recipe = Materials.GetRecipe(fbe.RecipeId);
                    if (fbe.store.MergetItem(2, recipe.Target[0].Copy()))
                    {
                        fbe.Pace = 0;
                        fbe.RecipeId = -1;
                    }
                }
                else
                {
                    fbe.Pace++;
                }
            }
            else
            {
                ItemStack fuelItem = fbe.store.GetItemStack(1);
                if (fuelItem != null)
                {
                    fbe.FuelTick = fuelItem.getMaterial().Fuel;
                    fbe.store.SubAmount(1, 1);
                }
                else
                {
                    e.block.SetState(0);
                }
            }
        }
    }
}